package com.example.webbackend.controller;

import com.example.webbackend.model.Group;
import com.example.webbackend.service.GroupService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/groups")
public class GroupController {
    private final GroupService groupService;

    // 添加构造器注入
    public GroupController(GroupService groupService) {
        this.groupService = groupService;
    }

    @PostMapping
    public Group createGroup(@RequestBody Group group) {
        return groupService.createGroup(group);
    }

    @GetMapping
    public List<Group> getAllGroups() {
        return groupService.getAllGroups();
    }

    @GetMapping("/categories")
    public List<String> getAllCategories() {
        return groupService.getAllCategories();
    }

    @PostMapping("/categories")
    public void addCategory(@RequestParam String category) {
        groupService.addCategory(category);
    }

    @DeleteMapping("/categories")
    public void removeCategory(@RequestParam String category) {
        groupService.removeCategory(category);
    }

    @GetMapping("/pending")
    public List<Group> getPendingGroups() {
        return groupService.getPendingGroups();
    }

    @GetMapping("/byCategory")
    public List<Group> getGroupsByCategory(@RequestParam String category) {
        return groupService.getGroupsByCategory(category);
    }

    @GetMapping("/search")
    public List<Group> searchGroups(@RequestParam String keyword) {
        return groupService.searchGroups(keyword);
    }

    @PostMapping("/{groupId}/join")
    public void joinGroup(@PathVariable Long groupId, @RequestParam Long userId) {
        groupService.joinGroup(groupId, userId);
    }

    @PostMapping("/{groupId}/leave")
    public void leaveGroup(@PathVariable Long groupId, @RequestParam Long userId) {
        groupService.leaveGroup(groupId, userId);
    }

    @PostMapping("/{groupId}/manage")
    public void manageMember(@PathVariable Long groupId,
                           @RequestParam Long memberId,
                           @RequestParam String action,
                           @RequestParam String adminUsername) {
        // 添加管理员权限检查
        if (!groupService.isGroupAdmin(groupId, adminUsername)) {
            throw new RuntimeException("无权执行此操作");
        }
        groupService.manageMember(groupId, memberId, action);
    }

    @PostMapping("/{groupId}/approve")
    public Group approveGroup(@PathVariable Long groupId,
                            @RequestParam String adminUsername) {
        // 添加管理员权限检查
        if (!groupService.isAdmin(adminUsername)) {
            throw new RuntimeException("无权执行此操作");
        }
        return groupService.approveGroup(groupId, adminUsername);
    }

    @DeleteMapping("/{groupId}")
    public void deleteGroup(@PathVariable Long groupId,
                          @RequestParam Long userId) {
        // 添加创建者检查
        if (!groupService.isCreator(groupId, userId)) {
            throw new RuntimeException("无权删除此小组");
        }
        groupService.deleteGroup(groupId, userId);
    }

    @PutMapping("/{groupId}/category")
    public Group updateGroupCategory(@PathVariable Long groupId,
                                   @RequestParam String category) {
        return groupService.updateGroupCategory(groupId, category);
    }
}